using System;
using System.Collections.Generic;
using System.Linq;

namespace GZ.Projects.WCS_YB
{
    /// <summary>
    /// 路径查找助手类 - 使用字典常量替代数据库查找next_stn
    /// </summary>
    public static class RouteHelper
    {
        #region 路径字典常量定义

        /// <summary>
        /// 路径映射字典 - 键格式: "CUR_STN|TO_STN", 值: RouteInfo对象
        /// 考虑到可维护性，后面可以加前置后置站台到RouteInfo中
        /// </summary>
        private static readonly Dictionary<string, RouteInfo> RouteMap = new Dictionary<string, RouteInfo>
        {
            // 1楼入库路径 - FORWARD方向
            { "101003|FORWARD", new RouteInfo { NextStn = "100000", Description = "1楼入库-FORWARD" } },
            { "101008|FORWARD", new RouteInfo { NextStn = "100000", Description = "1楼入库-FORWARD" } },
            { "101013|FORWARD", new RouteInfo { NextStn = "100000", Description = "1楼入库-FORWARD" } },
            // 2楼入库路径 - FORWARD方向
            { "201003|FORWARD", new RouteInfo { NextStn = "200000", Description = "2楼入库-FORWARD" } },
            { "201007|FORWARD", new RouteInfo { NextStn = "200000", Description = "2楼入库-FORWARD" } },
            { "201011|FORWARD", new RouteInfo { NextStn = "200000", Description = "2楼入库-FORWARD" } },
            // 1楼入库路径 - BACK方向
            { "101003|BACK", new RouteInfo { NextStn = "101005", Description = "1楼入库-BACK" } },
            { "101008|BACK", new RouteInfo { NextStn = "101010", Description = "1楼入库-BACK" } },
            { "101013|BACK", new RouteInfo { NextStn = "101015", Description = "1楼入库-BACK" } },
            // 2楼入库路径 - BACK方向
            { "201003|BACK", new RouteInfo { NextStn = "201004", Description = "2楼入库-BACK" } },
            { "201007|BACK", new RouteInfo { NextStn = "201008", Description = "2楼入库-BACK" } },
            { "201011|BACK", new RouteInfo { NextStn = "201012", Description = "2楼入库-BACK" } },
            // 2号巷道可用路径
            //入库

            { "RK01|TPK-05", new RouteInfo { NextStn = "101005", Description = "托盘库2巷道" } },
            { "TPK-05|101002", new RouteInfo { NextStn = "101001", Description = "托盘库5巷道" } },
            { "TPK-06|101002", new RouteInfo { NextStn = "101001", Description = "托盘库6巷道" } },
            { "TPK-07|101002", new RouteInfo { NextStn = "101001", Description = "托盘库3巷道" } },
            { "TPK-08|101002", new RouteInfo { NextStn = "101001", Description = "托盘库4巷道" } },
        };

        #endregion

        #region 公共方法

        /// <summary>
        /// 根据当前站台和目标方向获取下一站台信息
        /// </summary>
        /// <param name="curStn">当前站台编号</param>
        /// <param name="toStn">目标方向(FORWARD/BACK)或目标站台</param>
        /// <returns>路径信息，如果未找到返回null</returns>
        public static RouteInfo GetNextStation(string curStn, string toStn)
        {
            try
            {
                string key = string.Empty;
                if (string.IsNullOrEmpty(curStn) || string.IsNullOrEmpty(toStn))
                {
                    return null;
                }
                // 如果是TPK开头的站台，尝试前缀匹配
                if (curStn.StartsWith("TPK-") || curStn.StartsWith("LXK-"))
                {
                    var prefix = curStn.Substring(0, 6);
                    key = $"{prefix}|{toStn}";
                    if (RouteMap.ContainsKey(key))
                    {
                        return RouteMap[key];
                    }
                }
                if (toStn.StartsWith("TPK-") || toStn.StartsWith("LXK-"))
                {
                    var prefix = toStn.Substring(0, 6);
                    key = $"{curStn}|{prefix}";
                    if (RouteMap.ContainsKey(key))
                    {
                        return RouteMap[key];
                    }
                }
                // 直接匹配
                key = $"{curStn}|{toStn}";
                if (RouteMap.ContainsKey(key))
                {
                    return RouteMap[key];
                }


                return null;
            }
            catch (Exception ex)
            {
                Conn.YBLog.Error(1, "[RouteHelper.GetNextStation调用异常]", 1, $"获取下一站台失败: {ex.Message}");
                return null;
            }
        }

        /// <summary>
        /// 获取下一站台编号
        /// </summary>
        /// <param name="curStn">当前站台编号</param>
        /// <param name="toStn">目标方向或目标站台</param>
        /// <returns>下一站台编号，如果未找到返回空字符串</returns>
        public static string GetNextStn(string curStn, string toStn)
        {
            var routeInfo = GetNextStation(curStn, toStn);
            return routeInfo?.NextStn ?? string.Empty;
        }
        
        #endregion
    }

    /// <summary>
    /// 路径信息类
    /// </summary>
    public class RouteInfo
    {
        /// <summary>
        /// 下一站台编号
        /// </summary>
        public string NextStn { get; set; }

        /// <summary>
        /// 前置站台编号（可选）
        /// </summary>
        public string PreStn { get; set; }

        /// <summary>
        /// 后置站台编号（可选）
        /// </summary>
        public string PostStn { get; set; }

        /// <summary>
        /// 路径描述（可选）
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 路径优先级（数值越小优先级越高）
        /// </summary>
        public int Priority { get; set; } = 0;

        /// <summary>
        /// 路径状态（是否可用）
        /// </summary>
        public bool IsActive { get; set; } = true;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;

        public override string ToString()
        {
            return $"NextStn: {NextStn}, Description: {Description}, Priority: {Priority}, IsActive: {IsActive}";
        }
    }
}